﻿package cn.poco.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cn.poco.bean.Area;
import cn.poco.bean.City;
import cn.poco.bean.Dish;
import cn.poco.bean.Restaurant;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class CacheDbService {
	
	private CacheOpenHelper cacheopenhelper;
	
	public CacheDbService(Context context){
		cacheopenhelper = new CacheOpenHelper(context);
	}
	
/*	*//**
	 * @param dishs
	 * 保存图片集合URI
	 *//*
	public void saveUri(String url,String uri){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("insert into cache(url,uri) values(?,?)",
					new Object[]{url,uri});
		//db.close();
	}*/
	
/*	*//**
	 * @param citycode
	 * @return
	 * 根据url返回uri
	 *//*
	public String findUri(String url){
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from cache where url=?", new String[]{url});
		String uri = cursor.getString(cursor.getColumnIndex("uri"));
		cursor.close();
		finddb.close();
		return uri;
	}*/
/*	
	//获取数据库所有uri记录
	public HashMap<String,String> findAllUri(){
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from cache", null);
		 HashMap<String,String> uris = new HashMap<String, String>();
		while(cursor.moveToNext()){
			String url = cursor.getString(cursor.getColumnIndex("url"));
			String uri = cursor.getString(cursor.getColumnIndex("uri"));
			uris.put(url, uri);
		}
		cursor.close();
		finddb.close();
		return uris;
	}
	
	
	//删除记录，保留最新的200条记录
	public void deleteRecord(){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("delete  from cache where id not in  " +
				"(select id from cache order by id desc limit 200)");
		db.close();
	}
	
	//返回需要删除的图片路径集合
	public ArrayList<String> backDeleteRecord(){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor  = db.rawQuery("select *  from cache where id not in  " +
				"(select id from cache order by id desc limit 200)", null);
		ArrayList<String> paths = new ArrayList<String>();
		while(cursor.moveToNext()){
			String path = cursor.getString(cursor.getColumnIndex("uri"));
			paths.add(path);
		}
		cursor.close();
		db.close();
		return paths;
	}
	
	//返回uri记录条数
	public int checkRecordNum(){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from cache", null);
		int s  = cursor.getCount();
		cursor.close();
		db.close();
		return s;
	}*/
	
	
	
	
	/**
	 * 保存最近浏览餐厅
	 */
	public void savereview(String res_code,String res_title,String res_star,
			String res_average,String res_dish,String res_circle,String res_address,
			String res_tel,String res_main_dish,String res_comments,String res_images,
			String res_intro,String res_location,String res_join,String res_pepsi,
			String res_discount){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("insert into t_review " +
				"(res_code,res_title,res_star,res_average,res_dish,res_circle,res_address,res_tel," +
				"res_main_dish,res_comments,res_images,res_intro,res_location,res_join,res_pepsi," +
				"res_discount) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					new Object[]{res_code,res_title,res_star,res_average,res_dish,res_circle,res_address,
				res_tel,res_main_dish,res_comments,res_images,res_intro,res_location,res_join,res_pepsi,
				res_discount});
		db.close();
	}
	
	
	/**
	 * 清空最近浏览餐厅
	 */
	public void removeReView(){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		db.execSQL("delete from t_review");
		db.close();
	}
	
	/**
	 * 获取所有最近浏览餐厅列表
	 * @return
	 */
	public ArrayList<Restaurant> getReviewRestaurant(){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_review order by id desc",null);
		 ArrayList<Restaurant> resList = new ArrayList<Restaurant>();
		while(cursor.moveToNext()){
			Restaurant res = new Restaurant();
			String id = cursor.getString(cursor.getColumnIndex("res_code"));
			if(id!=null && !id.equals("")){
				res.setId(Integer.parseInt(id));
			}
			res.setTitle(cursor.getString(cursor.getColumnIndex("res_title")));
			res.setStar(cursor.getString(cursor.getColumnIndex("res_star")));
			res.setAverage(cursor.getString(cursor.getColumnIndex("res_average")));
			res.setDish(cursor.getString(cursor.getColumnIndex("res_dish")));
			res.setArea(cursor.getString(cursor.getColumnIndex("res_circle")));
			res.setAddress(cursor.getString(cursor.getColumnIndex("res_address")));
			res.setTel(cursor.getString(cursor.getColumnIndex("res_tel")));
			res.setMaindish(cursor.getString(cursor.getColumnIndex("res_main_dish")));
			String comment = cursor.getString(cursor.getColumnIndex("res_comments"));
			if(comment!=null && !comment.equals("")){
				res.setComment(Integer.parseInt(comment));
			}
			String images = cursor.getString(cursor.getColumnIndex("res_images"));
			if(images!=null && !comment.equals("")){
				res.setBlog(Integer.parseInt(images));
			}
			res.setIntro(cursor.getString(cursor.getColumnIndex("res_intro")));
			res.setLocation(cursor.getString(cursor.getColumnIndex("res_location")));
			String vouch = cursor.getString(cursor.getColumnIndex("res_join"));
			if(vouch!=null && !vouch.equals("")){
				res.setVouch(Integer.parseInt(vouch));
			}
			String pepsi =  cursor.getString(cursor.getColumnIndex("res_pepsi"));
			if(pepsi!=null && !pepsi.equals("")){
				res.setPepsi(Integer.parseInt(pepsi));
			}
			String discount =  cursor.getString(cursor.getColumnIndex("res_discount"));
			if(discount!=null && !discount.equals("")){
				res.setDiscount(Float.parseFloat(discount));
			}
			resList.add(res);
		}
		cursor.close();
		db.close();
		return resList;
	}
	
	
	/**
	 * 检查是否餐厅记录已经存在
	 * @param res_code
	 * @return
	 */
	public Integer checkReview(String res_code){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_review where res_code=?", new String[]{res_code});
		int count = cursor.getCount();
		cursor.close();
		db.close();
		return count;
	}
	
	
	/**
	 * @param dishs
	 * 保存菜单集合
	 */
	public void save(ArrayList<Dish> dishs){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		for(int i=0;i<dishs.size();i++){
			Dish dish = dishs.get(i);
			db.execSQL("insert into dish(citycode,dishid,dishname) values(?,?,?)",
					new Object[]{dish.getCitycode(),dish.getId(), dish.getName()});
		}
		db.close();
	}
	
	
	/**
	 * @param citycode
	 * @return
	 * 返回菜单集合
	 */
	public List<Dish> find(String citycode){
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from dish where citycode=?", new String[]{citycode});
		List<Dish> dishs = new ArrayList<Dish>();
			while(cursor.moveToNext()){
				Dish dish = new Dish();
				String code = cursor.getString(cursor.getColumnIndex("citycode"));
				Long dishid = cursor.getLong(cursor.getColumnIndex("dishid"));
				String dishname = cursor.getString(cursor.getColumnIndex("dishname"));
				dish.setCitycode(code);
				dish.setId(dishid);
				dish.setName(dishname);
				dishs.add(dish);
			}
		cursor.close();
		finddb.close();
		return dishs;
	}
	
	
	/**
	 * @param citycode
	 * @return
	 * 返回符合条件菜单记录数
	 */
	public Integer checkDish(String citycode){
		SQLiteDatabase finddb = cacheopenhelper.getReadableDatabase();
		Cursor cursor = finddb.rawQuery("select * from dish where citycode=?", new String[]{citycode});
		int count = cursor.getCount();
		cursor.close();
		finddb.close();
		return count;
	}
	
	
	/**
	 * 保存城市地区信息
	 * @param areaData
	 */
	public void saveArea(String citycode,List<Area> areas){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		for(int i=0;i<areas.size();i++){
			Area area = areas.get(i);
			db.execSQL("insert into area(area_id,name,city_code) values(?,?,?)"
					,new Object[]{area.getKey(),area.getName(),citycode});
		}
		db.close();
	}
	
	//获取城市地区数据
	public List<Area> getArea(String citycode){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where city_code=?", new String[]{citycode});
		List<Area> areas = new ArrayList<Area>();
		while(cursor.moveToNext()){
			Area area = new Area();
			area.setKey(cursor.getString(cursor.getColumnIndex("area_id")));
			area.setName(cursor.getString(cursor.getColumnIndex("name")));
			areas.add(area);
		}
		cursor.close();
		db.close();
		return areas;
	}
	
	
	//判断地区数据是否为空
	public Integer checkArea(String citycode){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where city_code=?", new String[]{citycode});
		int count = cursor.getCount();
		cursor.close();
		db.close();
		return count;
	}
	
	//根据区域ID返回区域名称
	public String getAreaNameById(String lid){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from area where area_id=?", new String[]{lid});
		if(cursor.moveToFirst()){
			lid = cursor.getString(cursor.getColumnIndex("name"));
		}
		cursor.close();
		db.close();
		return lid;
	}
	
	//保存城市信息
	public void saveCity(List<City> citys){
		SQLiteDatabase db = cacheopenhelper.getWritableDatabase();
		for(int i=0;i<citys.size();i++){
			db.execSQL("insert into city(city_code,name) values(?,?)", 
					new Object[]{citys.get(i).getKey(),citys.get(i).getName()});
		}
		db.close();
	}
	
	public List<City> findCity(){
		SQLiteDatabase db = cacheopenhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from city", null);
		List<City> citys = new ArrayList<City>();
		while(cursor.moveToNext()){
			City city = new City();
			city.setKey(cursor.getString(cursor.getColumnIndex("city_code")));
			city.setName(cursor.getString(cursor.getColumnIndex("name")));
			citys.add(city);
		}
		cursor.close();
		db.close();
		return citys;
	}

}
